
$(function() {
	$(".nav-ul li").eq(4).addClass("cur");
    if ($(".form-inline div").hasClass('form-more')) {
        $('.btn-more').show()
    }
    $('.btn-clear-form').click(function () {
        $('.form-inline input').val('')
    })
	 
	query(1);
	
	$('#pageNum').dropdown();
	$('#dropdown').on('shown.bs.dropdown', function () {
	     $('.dropdown-menu li').click(function () {
	         var str = $(this).children('a').html();
	         var size = $(this).children('a').children('label').text();
	         str += '<span class="caret"></span>';
	         $("#pageNum").html(str);
	         
	         query(1);
	     })
	 });
});

function jumpQuery(){
	var page = $("#jump_page").val();
	var reg = /^\+?[1-9][0-9]*$/;//判断是否为正整数 
    var flag = reg.test(trim(page));
    if(!flag){
    	layer.alert("请输入正整数！", {
			icon : 3,
			offset: '100px'
		});
    }else{
    	var totalPages = $("#total_pages").text();
    	if(parseInt(page) > parseInt(totalPages)){
    		layer.alert("跳转页数不能超过总页数！", {
    			icon : 3,
    			offset: '100px'
    		});
    	}else{
    		query(page);
    	}
    }
}

function query(page){
	layer.load(4);
	$.ajax({
		type : "POST",
		url :  "evalPassEnterprise/getEvalPassEnterpriseInfo",
		data : {
			"currentPage" : page,
			"enterprise_name" : $("#enterprise_name").val(),
			"registered_address" : $("#registered_address").val(),
			"form_number" : $("#form_number").val(),
			"form_statue" : $("#form_statue").val(),
			"production_unit" : $("#production_unit").val(),
			"bureau_address" : $("#bureau_address").val(),
			"apply_business" : $("#apply_business").val(),
			"userId" : $("#userId").val(),
			"pageSize" : $("#pageNum").children('strong').text()
		},
		dataType : "json",
		success : function(data) {
			layer.closeAll();
			if (data.flag) {
				var tread = data.datas;
				insertInfoTpl(tread, $('#tread'), $('#treadTpl'));
				var currentPage = data.currentPage;
				var totalPages = data.totalPages;
				var totalData = data.totalData;
				$("#total_data").text(totalData);
				$("#total_pages").text(totalPages);
				var options = {
					currentPage : currentPage, // 当前的请求页面。
					totalPages : totalPages, // 一共多少页。
					size : "normal", // 应该是页眉的大小。
					bootstrapMajorVersion : 3, // bootstrap的版本要求。
					alignment : "right",
					numberOfPages : 5, // 一页显示几个按钮
					itemTexts : function(type, page, current) { // 如下的代码是将页眉显示的中文显示我们自定义的中文。
						switch (type) {
						case "first":
							return "首页";
						case "prev":
							return "上一页";
						case "next":
							return "下一页";
						case "last":
							return "末页";
						case "page":
							return page;
						}
					},
					tooltipTitles : function(type, page, current) { // 提示框，当鼠标放上去给提示
						switch (type) {
						case "first":
							return "首页";
						case "prev":
							return "上一页";
						case "next":
							return "下一页";
						case "last":
							return "末页";
						}
					},
					onPageClicked : function(event, originalEvent, type, page) {
						layer.load(4);
						$.ajax({
							type : "POST",
							url :  "evalPassEnterprise/getEvalPassEnterpriseInfo",
							data : {
								"currentPage" : page,
								"enterprise_name" : $("#enterprise_name").val(),
								"registered_address" : $("#registered_address").val(),
								"form_number" : $("#form_number").val(),
								"form_statue" : $("#form_statue").val(),
								"production_unit" : $("#production_unit").val(),
								"bureau_address" : $("#bureau_address").val(),
								"apply_business" : $("#apply_business").val(),
								"userId" : $("#userId").val(),
								"pageSize" : $("#pageNum").children('strong').text()
							},
							dataType : "json",
							success : function(data) {
								if (data.flag) {
									layer.closeAll();
									var tread = data.datas;
									insertInfoTpl(tread, $('#tread'),$('#treadTpl'));
								}
							},
							error : function(data) {
								layer.closeAll();
							}
						});
					}
				}
				$('#pageLimit').bootstrapPaginator(options);
			}
		},
		error : function(data) {
			layer.closeAll();
		}
	});
}

function linkPage(id,form_type,apply_business,singleTaskId,value) {
	if(form_type == '1'){//老历史工单数据
		var modalBody = $("#modalBody");
		modalBody.empty();
		var str = "";
		if(id == 'house_node_num'){
		    str = "<div class='table-responsive table-box'>"+ 
				           "<table class='table table-hover table-bordered'>"+ 
				           "     <thead><tr><th>局址所在地</th></tr></thead>"+ 
				           "     <tbody><tr><td title="+value+" >"+value+"</td></tr></tbody>" + 
				           "</table>" +
				       "</div>";
		   
		}else{
			str = "<div class='table-responsive table-box'>"+ 
	           "<table class='table table-hover table-bordered'>"+ 
	           "     <thead><tr><th>链路总带宽(Gbps)</th></tr></thead>"+ 
	           "     <tbody><tr><td title="+value+" >"+value+"</td></tr></tbody>" + 
	           "</table>" +
	       "</div>";
		}
		 modalBody.append(str);
	}else{
		var userId = $("#userId").val();
		var htmlSrc = {
			house_node_num : 'houseNodeLink?singleTaskId='+singleTaskId+'&apply_business='+apply_business+'&userId='+userId,
			link_num : 'coverLink?singleTaskId='+singleTaskId+'&userId='+userId,
			contacts_info : 'contactsInfo?singleTaskId='+singleTaskId+'&userId='+userId
		}
		var iframeSrc = '';
		if (id == 'house_node_num') {
			iframeSrc = htmlSrc.house_node_num;
		}else if(id == 'link_num'){
			iframeSrc = htmlSrc.link_num;
		}else if(id == 'contacts_info'){
			iframeSrc = htmlSrc.contacts_info;
		}
		var iframeStr = '<iframe src=' + iframeSrc+ ' frameborder="0" ></iframe>';
		$('#modalBody').html(iframeStr);
	}
}

/**
 * [insertInfoTpl 插入 html信息]
 * 
 * @param {[type]}
 *            data [渲染所需的数据]
 * @param {[type]}
 *            dom [加入页面中的哪个DOM节点]
 * @return {[type]} [description]
 */
function insertInfoTpl(data, dom, tpl) {
	dom.empty();
    var infoTpl = tpl.html()
    var compiled = _.template(infoTpl)
    var html = ''
    _.forEach(data, function (item, index) {
        item.index = index + 1
        // console.log(item, index)
        html += compiled(item)
    })
    dom.append($(html))
}


function filter(name){
	if(name == null || name == "null" || name == undefined){
		return "";
	}else{
		return name
	}
}

function trim(str){ 
    return str.replace(/(^\s*)|(\s*$)/g, ""); 
}
